package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.domain.DanmoShoppingProductsCollection;
import com.muyu.system.service.IDanmoShoppingProductsCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品收藏Controller
 *
 * @author yangle
 * @date 2024-10-09
 */
@RestController
@RequestMapping("/collection")
public class DanmoShoppingProductsCollectionController extends BaseController
{
    @Autowired
    private IDanmoShoppingProductsCollectionService danmoShoppingProductsCollectionService;

    /**
     * 查询商品收藏列表
     */
//    @RequiresPermissions("system:collection:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        startPage();
        List<DanmoShoppingProductsCollection> list = danmoShoppingProductsCollectionService.selectDanmoShoppingProductsCollectionList(danmoShoppingProductsCollection);
        return getDataTable(list);
    }

    /**
     * 导出商品收藏列表
     */
//    @RequiresPermissions("system:collection:export")
    @Log(title = "商品收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        List<DanmoShoppingProductsCollection> list = danmoShoppingProductsCollectionService.selectDanmoShoppingProductsCollectionList(danmoShoppingProductsCollection);
        ExcelUtil<DanmoShoppingProductsCollection> util = new ExcelUtil<DanmoShoppingProductsCollection>(DanmoShoppingProductsCollection.class);
        util.exportExcel(response, list, "商品收藏数据");
    }

    /**
     * 获取商品收藏详细信息
     */
//    @RequiresPermissions("system:collection:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(danmoShoppingProductsCollectionService.selectDanmoShoppingProductsCollectionById(id));
    }

    /**
     * 新增商品收藏
     */
//    @RequiresPermissions("system:collection:add")
    @Log(title = "商品收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        return toAjax(danmoShoppingProductsCollectionService.insertDanmoShoppingProductsCollection(danmoShoppingProductsCollection));
    }

    /**
     * 修改商品收藏
     */
//    @RequiresPermissions("system:collection:edit")
    @Log(title = "商品收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        return toAjax(danmoShoppingProductsCollectionService.updateDanmoShoppingProductsCollection(danmoShoppingProductsCollection));
    }

    /**
     * 删除商品收藏
     */
//    @RequiresPermissions("system:collection:remove")
    @Log(title = "商品收藏", businessType = BusinessType.DELETE)
	@PostMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return danmoShoppingProductsCollectionService.deleteDanmoShoppingProductsCollectionByIds(ids);
    }

    /**
     * 添加商品收藏
     * @param danmoShoppingProductsCollection
     * @return
     */
    @PostMapping("/addCollection")
    public Result addCollection(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        danmoShoppingProductsCollection.setUserId(loginUser.getUserid());
        // 查询用户是否已经收藏过该商品
        DanmoShoppingProductsCollection danmoShoppingProductsCollectionLists = danmoShoppingProductsCollectionService.selectDanmoShoppingProductsCollectionLists(danmoShoppingProductsCollection);
        if (danmoShoppingProductsCollectionLists != null) {
            return Result.warn("商品已收藏");
        } else {
            danmoShoppingProductsCollectionService.addCollection(danmoShoppingProductsCollection);
            // 添加成功，返回成功信息
            return Result.success("商品收藏成功");
        }
    }

    /**
     * 取消商品收藏
     * @param danmoShoppingProductsCollection
     * @return
     */
    @PostMapping("/deleteCollection")
    public Result deleteCollection(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
                LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null){
            return Result.warn("请登录");
        }
        danmoShoppingProductsCollection.setUserId(loginUser.getUserid());
        // 尝试添加商品到收藏夹

        return  danmoShoppingProductsCollectionService.deleteDanmoById(danmoShoppingProductsCollection);


    }
    //查询用户收藏表
    @PostMapping("/listCollection")
    public Result listCollection(@RequestBody DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null){
            return Result.warn("请登录");
        }
        danmoShoppingProductsCollection.setUserId(loginUser.getUserid());
        List<DanmoShoppingProductsCollection> danmoShoppingProductsCollections = danmoShoppingProductsCollectionService.selectDmGoodsLists(danmoShoppingProductsCollection);
        return Result.success(danmoShoppingProductsCollections);
    }
}
